//
//  HeaderView.m
//  jingdongfang
//
//  Created by RKL on 2018/5/7.
//  Copyright © 2018年 BOE-SBG-CTO. All rights reserved.
//

#import "HeaderView.h"
#import "UIImageView+LBBlurredImage.h"
@interface HeaderView ()
/**
 背景图片
 */
@property (nonatomic, strong) UIImageView *bgImgView;
@end
@implementation HeaderView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        [self setUpUI];
    }
    return self;
}
#pragma mark - 构建UI
- (void)setUpUI
{
    UIImage *iconImage = [UIImage imageFromColor:[UIColor mainRandomColor]];//默认图片
    
    //背景
    _backgroundImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, -kNavBarHeight - KHeadViewOff, self.bounds.size.width,
                                                                         self.bounds.size.height + kNavBarHeight +KHeadViewOff)];  //向上拉伸navbar 高度
    [_backgroundImageView setImageToBlur:iconImage blurRadius:20 completionBlock:nil]; //高斯处理
    [self addSubview:_backgroundImageView];
    _backgroundImageView.contentMode = UIViewContentModeScaleToFill;
    
    //左侧图片
    UIImageView * leftView = [[UIImageView alloc] initWithFrame:CGRectMake(15, 80 - kNavBarHeight, 139, 139)];
    leftView.image = iconImage;
    leftView.userInteractionEnabled = YES;
    _leftView = leftView;
    _leftView.layer.masksToBounds = YES;
    [_leftView.layer setCornerRadius:10]; //设置矩形四个圆角半径
    [self addSubview:leftView];
    
    UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(leftViewClick)];
    //设置成NO表示当前控件响应后会传播到其他控件上，默认为YES。
    tapGestureRecognizer.cancelsTouchesInView = NO;
    //将触摸事件添加到view上
    [_leftView addGestureRecognizer:tapGestureRecognizer];
    
    
    
    self.lookLabel = ({
        UILabel * label = [[UILabel alloc] init];
        label.text = @"2222万";
        label.frame = CGRectMake(139-15-[self calculateRowWidth:label.text], 8, [self calculateRowWidth:label.text], 12);
        label.textColor = RGB(255, 255, 255);
        label.font = [UIFont systemFontOfSize:12];
        label.textAlignment = NSTextAlignmentCenter;
        label;
    });
    [_leftView addSubview:self.lookLabel];
    
    self.lookImageView = ({
        UIImageView * imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"picture_detail_info"]];
        imageView.frame = CGRectMake(CGRectGetMinX(self.lookLabel.frame)-20, 8.5, 15, 11);
        imageView;
    });
    [_leftView addSubview:self.lookImageView];
    
    //label
    UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(leftView.frame)+15, CGRectGetMinY(leftView.frame)+24, KSCREENWIDTH-CGRectGetMaxX(leftView.frame)-30, 18)];
    label.text = @"动人情话遇见申请告白";
    label.textColor = RGB(255, 255, 255);
    _titleLabel = label;
    [self addSubview:label];
    
    
    UIButton * authorBtn = ({
        UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(CGRectGetMaxX(leftView.frame)+15, CGRectGetMaxY(_titleLabel.frame)+24, 150, 24);
        button.backgroundColor = [UIColor clearColor];
        [button addTarget:self action:@selector(authorBtnClick) forControlEvents:UIControlEventTouchUpInside];
        button;
    });
    [self addSubview:authorBtn];
    
    self.authorImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 24, 24)];
    self.authorImageView.userInteractionEnabled = YES;
    [self.authorImageView.layer setCornerRadius:12]; //设置矩形四个圆角半径
    [self.authorImageView.layer setBorderWidth:0]; //边框宽度
    [authorBtn addSubview:self.authorImageView];
    
    self.authorLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.authorImageView.frame)+5, 0, 100, 24)];
    self.authorLabel.text = @"zhuzhidaong";
    self.authorLabel.textColor = RGB(255, 255, 255);
    [authorBtn addSubview:self.authorLabel];
    
    
    float tempWidth = (KSCREENWIDTH-100)/8;
    self.btn1 = ({
        UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(tempWidth, CGRectGetMaxY(_leftView.frame)+19, 25, 25);
        [button setImage:[UIImage imageNamed:@"picture_detail_unlike"] forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:@"picture_detail_like"] forState:UIControlStateSelected];
        [button addTarget:self action:@selector(button1Click:) forControlEvents:UIControlEventTouchUpInside];
        button;
    });
    [self addSubview:self.btn1];

    self.btn2 = ({
        UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(CGRectGetMaxX(self.btn1.frame)+2*tempWidth, CGRectGetMaxY(_leftView.frame)+19, 25, 25);
        [button setImage:[UIImage imageNamed:@"picture_pinglun_bai"] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(button2Click:) forControlEvents:UIControlEventTouchUpInside];

        button;
    });
    [self addSubview:self.btn2];

    self.btn3 = ({
        UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(CGRectGetMaxX(self.btn2.frame)+2*tempWidth, CGRectGetMaxY(_leftView.frame)+19, 25, 25);
        [button setImage:[UIImage imageNamed:@"picture_fenxiang"] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(button3Click:) forControlEvents:UIControlEventTouchUpInside];

        button;
    });
    [self addSubview:self.btn3];

    self.btn4 = ({
        UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(CGRectGetMaxX(self.btn3.frame)+2*tempWidth, CGRectGetMaxY(_leftView.frame)+19, 25, 25);
        [button setImage:[UIImage imageNamed:@"picture_ts_bai"] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(button4Click:) forControlEvents:UIControlEventTouchUpInside];
        button;
    });
    [self addSubview:self.btn4];

    
    self.btnLabel1 = ({
        UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.btn1.frame)+6, KSCREENWIDTH/4, 12)];
        label.text = @"2222";
        label.textColor = RGB(255, 255, 255);
        label.font = [UIFont systemFontOfSize:12];
        label.textAlignment = NSTextAlignmentCenter;
        label;
    });
    [self addSubview:self.btnLabel1];
    
    self.btnLabel2 = ({
        UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(KSCREENWIDTH/4, CGRectGetMaxY(self.btn1.frame)+6, KSCREENWIDTH/4, 12)];
        label.text = @"2222";
        label.textColor = RGB(255, 255, 255);
        label.font = [UIFont systemFontOfSize:12];
        label.textAlignment = NSTextAlignmentCenter;
        label;
    });
    [self addSubview:self.btnLabel2];
    
    self.btnLabel3 = ({
        UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(KSCREENWIDTH/4*2, CGRectGetMaxY(self.btn1.frame)+6, KSCREENWIDTH/4, 12)];
        label.text = @"2222";
        label.textColor = RGB(255, 255, 255);
        label.font = [UIFont systemFontOfSize:12];
        label.textAlignment = NSTextAlignmentCenter;
        label;
    });
    [self addSubview:self.btnLabel3];
    
    self.btnLabel4 = ({
        UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(KSCREENWIDTH/4*3, CGRectGetMaxY(self.btn1.frame)+6, KSCREENWIDTH/4, 12)];
        label.text = @"推送";
        label.textColor = RGB(255, 255, 255);
        label.font = [UIFont systemFontOfSize:12];
        label.textAlignment = NSTextAlignmentCenter;
        label;
    });
    [self addSubview:self.btnLabel4];
    
}

#pragma markk - set方法
- (void)setLookStr:(NSString *)lookStr
{
    _lookStr = lookStr;
    _lookLabel.text = lookStr;
    _lookLabel.frame = CGRectMake(139-15-[self calculateRowWidth:_lookLabel.text], 8, [self calculateRowWidth:_lookLabel.text], 12);
    _lookImageView.frame = CGRectMake(CGRectGetMinX(self.lookLabel.frame)-20, 8.5, 15, 11);
}

- (void)setTitleLabel:(UILabel *)titleLabel
{
    _titleLabel = titleLabel;
    
}

- (void)setLeftView:(UIImageView *)leftView
{
    _leftView = leftView;
}

- (void)setIsLike:(BOOL)isLike
{
    _isLike = isLike;
    
}

#pragma mark - 图片点击事件
- (void)leftViewClick
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(clickLeftView)])
    {
        [self.delegate clickLeftView];
    }
}
#pragma mark - 作者点击事件
- (void)authorBtnClick
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(clickAuthorButtonClick)])
    {
        [self.delegate clickAuthorButtonClick];
    }
}

#pragma mark - 计算lable的宽度
- (CGFloat)calculateRowWidth:(NSString *)string
{
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:12]};  //指定字号
    CGRect rect = [string boundingRectWithSize:CGSizeMake(0, 20)/*计算宽度时要确定高度*/ options:NSStringDrawingUsesLineFragmentOrigin |
                   NSStringDrawingUsesFontLeading attributes:dic context:nil];
    return rect.size.width;
}

#pragma mark - 处理图片的方法  网络请求回来后调用
- (void)setIconPicStr:(NSString *)iconPicStr withCompleteBlock:(picCompleteBlock) completion
{
    //回调这里
    if (iconPicStr)
    {
        [self.leftView sd_setImageWithURL:[NSURL URLWithString:iconPicStr] placeholderImage:[UIImage imageFromColor:[UIColor mainRandomColor]] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL)
         {
             [self.backgroundImageView setImageToBlur:image blurRadius:20 completionBlock:^{
                 completion();
             }]; //高斯处理
         }];
    }
    else
    {
        UIImage *iconImage = [UIImage imageNamed:@"B-16-2"]; //换成sd
        self.leftView.image = iconImage;
        [self.backgroundImageView setImageToBlur:iconImage blurRadius:20 completionBlock:^{
            completion();
        }]; //高斯处理
    }
}

#pragma mark - 关注按钮的点击事件
- (void)button1Click:(UIButton * )sender
{
    sender.selected = !sender.selected;
    if (_isLike)
    {
        if ([self.delegate respondsToSelector:@selector(cancleFavoriteClick)])
        {
            [self.delegate cancleFavoriteClick];
        }
    }
    else
    {
        if ([self.delegate respondsToSelector:@selector(addFavoriteClick)])
        {
            [self.delegate addFavoriteClick];
        }
    }
    _isLike = !_isLike;
}

#pragma mark - 评论按钮点击事件
- (void)button2Click:(UIButton *)sender
{
    if ([self.delegate respondsToSelector:@selector(pinglunClick)])
    {
        [self.delegate pinglunClick];
    }
}

#pragma mark - 分享按钮点击事件
- (void)button3Click:(UIButton *)sender
{
    if ([self.delegate respondsToSelector:@selector(shareClick)])
    {
        [self.delegate shareClick];
    }
}

#pragma mark - 推送按钮点击事件
- (void)button4Click:(UIButton *)sender
{
    if ([self.delegate respondsToSelector:@selector(pushClick)])
    {
        [self.delegate pushClick];
    }
}

@end










